<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-30 10:05
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Transformers;


use function foo\func;
use League\Fractal\TransformerAbstract;

class GetBarberServiceListTransformer extends TransformerAbstract
{
	private $list;
	private $is_live;

	public function __construct($list,$is_live=false)
	{
		$this->list = $list;
		$this->is_live = $is_live;
	}

	public function transformer(){
		$listEmpty = $this->list->isEmpty();
		if($listEmpty){
			$newList = null;
		}else{
			if($this->is_live){
				$listArr = $this->list->toArray();
				$newList['list'] = array_map(function ($v){
					$v['store_service']['category'] = isset($v['store_service']['category']) ? $v['store_service']['category'] : null;
					return $v['store_service'];
				},$listArr);

				$newList['appointment_total_counts'] = array_reduce($listArr,function ($counts,$v){
					return $counts += isset($v['store_service']['appointment_count']) ? $v['store_service']['appointment_count'] : 0;
				},0);

			}else{
				foreach ($this->list as $v){
					$newList[] = isset($v->storeService) ?  [
						'service_id' => $v->storeService->id,
						'service_name' => $v->storeService->name,
						'service_price' => $v->storeService->price
					] : null;
				}
			}

		}
		return $newList;
	}
}
